package com.sxz.sbs.module.client;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.adapter.DealRecordAdapter;
import com.sxz.sbs.module.client.bean.DealRecordBean;
import com.sxz.sbs.module.client.presenter.DealRecordPresenter;
import com.sxz.sbs.module.client.view.DealRecordView;
import com.sxz.sbs.widget.CommonTitle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import butterknife.BindView;

/**
 * @author hp
 * @date 2018/1/22
 * 交易记录
 */

public class DealRecordActivity extends BaseActivity<DealRecordPresenter> implements DealRecordView {

    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private DealRecordAdapter dealRecordAdapter;
    private List<DealRecordBean.ListBean> mlist=new ArrayList<>();
    private int currentPage = 1;
    private int customId;
    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("交易记录").build().show();
        customId = getIntent().getIntExtra("customId",0);
        dealRecordAdapter = new DealRecordAdapter(mlist);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(dealRecordAdapter);
        /**
         * 下拉刷新
         */
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setRefreshing(true);
        getPlanListData();
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getPlanListData();
            }
        });
        dealRecordAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage ++;
                getPlanListData();
            }
        },recyclerView);

        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent=new Intent(DealRecordActivity.this,DealScheduleActivity.class);
                intent.putExtra("customId",customId);
                intent.putExtra("deal_record_detail",mlist.get(position));
                startActivity(intent);
            }
        });
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                getPlanListData();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dealrecord;
    }

    @Override
    protected DealRecordPresenter createPresenter() {
        return new DealRecordPresenter();
    }


    @Override
    public void onPlanListSuccess(final DealRecordBean data) {
        if (mIsLive && data != null) {
            refreshLayout.setRefreshing(false);
            if (data.getList() != null && data.getList().size() >0) {
                stateLayout.showContent();
                if (currentPage == 1) {
                    mlist.clear();
                    mlist.addAll(data.getList());
                    dealRecordAdapter.notifyDataSetChanged();
                } else {
                    mlist.addAll(data.getList());
                    dealRecordAdapter.notifyDataSetChanged();
                }
                dealRecordAdapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    mlist.clear();
                    dealRecordAdapter.notifyDataSetChanged();
                    //  数据为空
                    stateLayout.showEmpty();
                } else {
                    if (mlist.size() > AppConstant.PAGE_SIZE) {
                        dealRecordAdapter.loadMoreEnd();
                    } else {
                        dealRecordAdapter.loadMoreEnd(true);
                    }

                }
            }
        }
    }
    @Override
    public void loginAgain() {
        super.loginAgain();
        refreshLayout.setRefreshing(false);
        dealRecordAdapter.loadMoreFail();
    }
    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
        if (currentPage == 1) {
            if (errorTag == AppConstant.NET_ERROR_CODE) {
                stateLayout.showNetwork();
            } else {
                stateLayout.showEmpty();
            }
        } else {
            dealRecordAdapter.loadMoreFail();
        }
    }
    private void getPlanListData(){
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("customId", customId);
        map.put("pageNumber", currentPage);
        map.put("pageSize", AppConstant.PAGE_SIZE);
        mPresenter.getPlanList(map);
    }
}
